package com.blb.community.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.community.Annotation.Log;
import com.blb.community.entity.SysDictType;
import com.blb.community.entity.vo.DictTypeVo;
import com.blb.community.service.ISysDictTypeService;
import com.blb.community.utils.ResponseResult;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 字典类型表 前端控制器
 * </p>
 *
 * @author 帅帅
 * @since 2022-07-02
 */
@Api(tags = "字典类型管理模块")
@RestController
@CrossOrigin
@RequestMapping("/sys-dict-type")
public class SysDictTypeController {

    @Autowired
    private ISysDictTypeService sysDictTypeServicei;


    /**
     * 查询所有字典类型
     *
     * @return
     */
    @ApiOperation("查询所有字典类型")
    @GetMapping("/dictTypeList")
    public ResponseResult<List<SysDictType>> getAllDictType() {
        List<SysDictType> list = sysDictTypeServicei.list();
        return ResponseResult.ok(list);
    }

    /**
     * 分页查询字典类型列表
     *
     * @return
     */
    @ApiOperation("获取字典类型列表")
    @GetMapping("/dictTypes/dictType")
    public ResponseResult<IPage<SysDictType>> getDictTypes(Long current, DictTypeVo dictTypeVo) {
        IPage<SysDictType> dictTypePage = sysDictTypeServicei.getDictTypePage(dictTypeVo, new Page<>(dictTypeVo.getCurrent(), 100L));
        return ResponseResult.ok(dictTypePage);
    }

    /**
     * 分页查询字典类型列表
     *
     * @return
     */
    @ApiOperation("分页查询字典类型列表")
    @GetMapping("/dictTypes/page")
     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    public ResponseResult<IPage<SysDictType>> getDictTypeAll(Long current, DictTypeVo dictTypeVo) {
        LocalDateTime createTime = dictTypeVo.getCreateTime();
        System.out.println(createTime);
        IPage<SysDictType> dictTypePage = sysDictTypeServicei.getDictTypePage(dictTypeVo, new Page<>(dictTypeVo.getCurrent(), 8L));
        return ResponseResult.ok(dictTypePage);
    }

    @Log(module = "添加字典类型", operationType = 1, requestMethod = "POST")
    @ApiOperation("添加字典类型")
    @PostMapping("/dictType")
    public ResponseResult<String> addDictType(SysDictType dictType) {
        String dictName1 = dictType.getDictName();
        String dictType1 = dictType.getDictType();
        List<SysDictType> list = sysDictTypeServicei.list();
        for (int i = 0; i < list.size(); i++) {
            if (dictName1.equals(list.get(i).getDictName())) {
                System.out.println("字典名称重复");
                return ResponseResult.ok("字典名称重复");
            } else if (dictType1.equals(list.get(i).getDictType())) {
                System.out.println("字典类型重复");
                return ResponseResult.ok("字典类型重复");
            }
        }
        LocalDateTime now = LocalDateTime.now();
        dictType.setCreateTime(now);
        sysDictTypeServicei.save(dictType);
        return ResponseResult.ok("添加成功");
    }

    @Log(module = "修改字典类型", operationType = 2, requestMethod = "PUT")
    @ApiOperation("修改字典类型")
    @PutMapping("/dictType")
    public ResponseResult<String> updateDictType(@RequestBody SysDictType dictType) {
        LocalDateTime now = LocalDateTime.now();
        dictType.setUpdateTime(now);
        sysDictTypeServicei.updateById(dictType);
        return ResponseResult.ok("修改成功");
    }
    @Log(module = "单个逻辑删除字典类型", operationType = 3, requestMethod = "DELETE")
    @ApiOperation("单个逻辑删除字典类型")
    @DeleteMapping("/dictType/{dictId}")
    public ResponseResult<String> deleteDictType(@PathVariable Long dictId) {
        sysDictTypeServicei.logicDeleteDictType(dictId);
        return ResponseResult.ok("删除成功");
    }
    @Log(module = "批量删除字典类型信息", operationType = 3, requestMethod = "DELETE")
    @ApiOperation("批量删除字典类型信息")
    @DeleteMapping("/dictTypes/{arr}")
    public ResponseResult<String> deleteDictTypes(@PathVariable ArrayList<Long> arr) {
        sysDictTypeServicei.removeByIds(arr);
        return ResponseResult.ok("批量删除成功");
    }
}
